﻿package Core 
{
	import com.junkbyte.console.Cc;
	import Common.Constants;
	import Common.ImageInfo;
	import Common.Preloader;
	import Events.ImageEvent;
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.HTTPStatusEvent;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.geom.Point;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import Network.XMLLoader;
	/**
	 * ...
	 * @author Serg de Adelantado
	 */
	public class RemoteImage extends ImageContainer 
	{	
		private var FInfo:ImageInfo;
		private var FLinkToImage:URLRequest;
		private var FLoader:Loader;
		private var FRemotePreloader:Preloader;		
		
		public function RemoteImage(AWidth:int, AHeight:int) 
		{			
			super(AWidth, AHeight);	
			FIsLocal = false;
			FOrder = -1;
		}
		
		override public function Load():void
		{		
			if (FLinkToImage)
			{
				FLoader = new Loader();
				FLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, OnImageLoaded);
				FLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, OnIOerror);
				FLoader.load(FLinkToImage);
				FRemotePreloader = new Preloader();
				FRemotePreloader.x = (FSize.x - FRemotePreloader.width) / 2;
				FRemotePreloader.y = (FSize.y - FRemotePreloader.height) / 2;
				addChild(FRemotePreloader);
				Cc.logch('Remote image', 'Loading image at URL: ' + FLinkToImage.url);
			}
			else
				Cc.errorch('Remote image', 'Can not load image: URL was not assigned');						
		}
		
		private function OnIOerror(e:IOErrorEvent):void 
		{
			if (FRemotePreloader)
			{
				removeChild(FRemotePreloader);
				FRemotePreloader = null;				
			}
			Cc.errorch('Remote image', 'IO error during image loading');
			ProcessBitmap(new Bitmap(new NoImageIcon()));
		}
		
		private function OnImageLoaded(e:Event):void 
		{
			e.target.removeEventListener(Event.COMPLETE, OnImageLoaded);
			ProcessBitmap(Bitmap(e.target.content));
		}
		
		override protected function ProcessBitmap(ABitmap:Bitmap):void 
		{
			super.ProcessBitmap(ABitmap);
			if (FRemotePreloader)
			{
				removeChild(FRemotePreloader);
				FRemotePreloader = null;
			}
			if (FLoader && FLoader.contentLoaderInfo.bytesLoaded > 0)
			{
				FLoader.contentLoaderInfo.bytes.clear();
				FLoader.unloadAndStop();
				FLoader = null;
			}
			FIsLoaded = true;
			dispatchEvent(new ImageEvent(ImageEvent.IMAGE_LOADED));
		}
		
		override public function UploadImage():void 
		{
			super.UploadImage();
		}
		
		//{ Getters & setters 				
		
		public function get Info():ImageInfo 
		{
			return FInfo;
		}
		
		public function set Info(AInfo:ImageInfo):void 
		{
			FInfo = AInfo;
			FID = FInfo.ID;
			Description = FInfo.Description;
			MarkedForDelete = FInfo.ToDelete;			
			ObjectID = FInfo.ObjectID;
			FLinkToImage = new URLRequest(ImageURL());			
		}
		
		public function get BigImageURL():String 
		{
			return ImageURL(false);
		}
		
		private function ImageURL(ASmall:Boolean = true):String
		{
			var vFile:String = Constants.IMAGES_MAIN;
			if (FOwnerType == Constants.OWNER_ROOM)
				vFile = Constants.IMAGES_ROOMS;
			var vSmall:String = '';
			if (ASmall)
				vSmall = 'sm_';			
			vFile += vSmall + FInfo.ID.toString() + '.jpg'; 	
			return vFile;
		}
		
		override public function get TipText():String		
		{
			var vResult:String = "<font color='" + '#666666' + "'>" + 'Сетевой файл' + "</font>" + '\n';
			return vResult;
		}		
		
		//} endregion
		//End of getters & setters
	}
}